import datetime
import time

from django.http import JsonResponse
from django.utils.deprecation import MiddlewareMixin
from app.models import *

class MD1(MiddlewareMixin):
    def process_request(self, request):
        if '/app/login' not in request.path:
            token = request.META.get("HTTP_AUTHORIZATION")
            if not token:
                return JsonResponse ({"code": 403, "message": "token无效"})
            try:
                user = UserInfo.objects.filter(tokens=token.split(' ')[1]).all().values()
                if float(user[0]['end_token'])<time.time():
                    return JsonResponse({"code": 401, "message": "token过期，重新登录"})
            except IndexError:
                return JsonResponse({"code": 403, "message": "token无效"})
